<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户编号" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户编号"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="收入" prop="shouRu">
        <el-input v-model="dataForm.shouRu" placeholder="收入"></el-input>
      </el-form-item>
      <el-form-item label="支出" prop="zhiChu">
        <el-input v-model="dataForm.zhiChu" placeholder="支出"></el-input>
      </el-form-item>

<!--      <el-form-item label="时间" prop="createTime">-->
<!--        <el-input v-model="dataForm.createTime" placeholder="时间"></el-input>-->
<!--      </el-form-item>-->

      <el-form-item label="详情" prop="memo">
        <el-input v-model="dataForm.memo" placeholder="详情"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        userId: '',
        memo: '',
        username: '',
        shouRu: '',
        zhiChu: '',
        createTime: ''
      },
      dataRule: {
        userId: [
          {required: true, message: '用户编号不能为空', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '用户名不能为空', trigger: 'blur'}
        ],
        shouRu: [
          {required: true, message: '收入不能为空', trigger: 'blur'}
        ],
        zhiChu: [
          {required: true, message: '支出不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/jsf/jsftotalmoney/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.jsfTotalMoney.userId
              this.dataForm.username = data.jsfTotalMoney.username
              this.dataForm.shouRu = data.jsfTotalMoney.shouRu
              this.dataForm.zhiChu = data.jsfTotalMoney.zhiChu
              this.dataForm.memo = data.jsfTotalMoney.memo
              this.dataForm.createTime = data.jsfTotalMoney.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/jsf/jsftotalmoney/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'userId': this.dataForm.userId,
              'username': this.dataForm.username,
              'memo': this.dataForm.memo,
              'shouRu': this.dataForm.shouRu,
              'zhiChu': this.dataForm.zhiChu,
              'createTime': this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
